/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.otaliastudios.cameraview.engine.qnx;

import static com.otaliastudios.cameraview.engine.qnx.CameraStateConstants.*;

import android.util.Log;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * camera qnx侧状态监听线程类
 */
public class CameraStateObserverThread extends Thread {
    public static final String TAG = "CameraStateObserverThread";
    int mLastStateValue = DVR_STATE_READY;
    int mCurStateValue = DVR_STATE_READY;

    EventListerner mEventListerner = null;
    String value = "";
    volatile boolean needRunning = true;

    /**
     * 停止线程
     */
    public void stopThread() {
        Log.i(TAG, "stopThread");
        needRunning = false;
    }

    /**
     * 注册事件监听回调
     */
    public void registerEventListener(EventListerner eventListerner) {
        mEventListerner = eventListerner;
    }

    /**
     * 注销事件监听回调
     */
    public void unRregisterEventListener() {
        mEventListerner = null;
    }

    @Override
    public void run() {
        FileReader fileReader;
        BufferedReader bufferedReader = null;
        while (needRunning) {
            try {
                Thread.sleep(MONITOR_WAIT_TIME);
            } catch (InterruptedException e) {
                Log.i(TAG, "run: sleep InterruptedException.");
            }
            try {
                fileReader = new FileReader(DVR_STATUS_PATH);

            } catch (FileNotFoundException e) {
                Log.i(TAG, "run: invalid path");
                needRunning = false;
                continue;
            }
            try {
                bufferedReader = new BufferedReader(fileReader);
                value = (String) bufferedReader.readLine();
                try {
                    mCurStateValue = Integer.decode(value);
                    Log.i(TAG, "run: mCurStateValue" + mCurStateValue);
                } catch (NumberFormatException e) {
                    Log.i(TAG, "run: NumberFormatException.");
                }

                if (mEventListerner == null) {
                    Log.i(TAG, "run:mEventListerner is null");
                    needRunning = false;
                    continue;
                }

                if ((mLastStateValue == DVR_STATE_NOT_READY) && (mCurStateValue == DVR_STATE_READY)) {
                    mEventListerner.onDvrReadyChange(true);
                }
                if ((mLastStateValue == DVR_STATE_READY) && (mCurStateValue == DVR_STATE_NOT_READY)) {
                    mEventListerner.onDvrReadyChange(false);
                }
                mLastStateValue = mCurStateValue;
            } catch (IOException e) {
                Log.i(TAG, "run: IOException.");
            } finally {
                if (bufferedReader != null) {
                    safeClose(bufferedReader);
                }
            }
        }
    }

    public static void safeClose(BufferedReader br) {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                Log.i(TAG, "run:IOException when close BufferedReader");
            }
        }
    }
}
